package com.ydy.permission.service.impl;

import com.ydy.common.constant.Constant;
import com.ydy.common.model.Common;
import com.ydy.common.model.Dict;
import com.ydy.common.util.RedisUtil;
import com.ydy.common.vo.UserRoleMenuPermissionVo;
import com.ydy.permission.mapper.PermissionMapper;
import com.ydy.permission.service.PermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RedisTemplate<Serializable, Object> redisTemplate;

    @Override
    public List<UserRoleMenuPermissionVo> queryUserRole() {
        List<UserRoleMenuPermissionVo>  list = new LinkedList<>();

        List<UserRoleMenuPermissionVo> parentList = permissionMapper.queryParentUserRoleInfo();
        List<Common> childrenList = permissionMapper.queryCommonRoleInfo();

        return getResultList(parentList,childrenList);
    }

    @Override
    public List<UserRoleMenuPermissionVo> queryRoleMenus() {
        List<UserRoleMenuPermissionVo> parentList = permissionMapper.queryParentRoleMenusInfo();
        List<Common> childrenList = permissionMapper.queryCommonMenusInfo();

        return getResultList(parentList,childrenList);
    }

    @Override
    public List<UserRoleMenuPermissionVo> queryRolePermission() {
        List<UserRoleMenuPermissionVo>  list = new LinkedList<>();

        List<UserRoleMenuPermissionVo> parentList = permissionMapper.queryParentRolePermissionInfo();
        List<Common> childrenList = permissionMapper.queryCommonPermissionInfo();

        return getResultList(parentList,childrenList);
    }

    @Override
    @Transactional
    public void saveUserRole(List<Map<String, String>> addList, List<Map<String, String>> delList) {
        //删除
        if(delList.size() > 0)permissionMapper.deleteUserRole(delList);
        //插入
        if(addList.size() > 0)permissionMapper.insertUserRole(addList);
    }

    @Override
    @Transactional
    public void saveRoleMenu(List<Map<String, String>> addList, List<Map<String, String>> delList) {
        //删除
        if(delList.size() > 0)permissionMapper.deleteRoleMenu(delList);
        //插入
        if(addList.size() > 0)permissionMapper.insertRoleMenu(addList);
    }

    @Override
    @Transactional
    public void saveRolePermission(List<Map<String, String>> addList, List<Map<String, String>> delList) {
        //删除
        if(delList.size() > 0)permissionMapper.deleteRolePermission(delList);
        //插入
        if(addList.size() > 0)permissionMapper.insertRolePermission(addList);
    }

    private List<UserRoleMenuPermissionVo> getResultList(List<UserRoleMenuPermissionVo> parentList, List<Common> childrenList) {
        List<UserRoleMenuPermissionVo>  list = new LinkedList<>();
        parentList.forEach(parentValue ->{
            UserRoleMenuPermissionVo parentVo = new UserRoleMenuPermissionVo(parentValue.getId(),parentValue.getLabel());
            String childrenId = parentValue.getChildrenId();
            Set<UserRoleMenuPermissionVo> children = new TreeSet<>();
            childrenList.forEach(common -> {
                UserRoleMenuPermissionVo childrenVo = new UserRoleMenuPermissionVo(parentValue.getId() + "-" + common.getId(),common.getName(),"0");
                if(StringUtils.isNotEmpty(childrenId) && childrenId.contains(common.getId())){
                    childrenVo.setIsCheck("1");
                }
                children.add(childrenVo);
            });
            parentVo.setChildren(children);
            list.add(parentVo);
        });
        return list;
    }

    @Override
    public List<Dict> queryDictList() {
        return permissionMapper.queryDictList();
    }
}
